package com.qfedu.fy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.fy.dao.DepartmentDao;
import com.qfedu.fy.dao.UserDao;
import com.qfedu.fy.dto.UserDetailDto;
import com.qfedu.fy.dto.UserLoginDto;
import com.qfedu.fy.entity.Department;
import com.qfedu.fy.entity.User;
import com.qfedu.fy.service.intf.DepartmentService;
import com.qfedu.fy.service.intf.UserService;
import com.qfedu.fy.vo.PageBeanVo;
import com.qfedu.fy.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * ......................我佛慈悲......................
 * _oo0oo_
 * o8888888o
 * 88" . "88
 * (| -_- |)
 * 0\  =  /0
 * ___/`---'\___
 * .' \\|     |// '.
 * / \\|||  :  |||// \
 * / _||||| -卍-|||||- \
 * |   | \\\  -  /// |   |
 * | \_|  ''\---/''  |_/ |
 * \  .-\__  '-'  ___/-. /
 * ___'. .'  /--.--\  `. .'___
 * ."" '<  `.___\_<|>_/___.' >' "".
 * | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * \  \ `_.   \_ __\ /__ _/   .-` /  /
 * =====`-.____`.___ \_____/___.-`___.-'=====
 * `=---='
 * ..............佛祖保佑无BUG..................
 * @author mxj
 * @date 2022/6/16 20:30
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao dao;


    @Override
    public R add(User user) {
        if(dao.add(user)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageBeanVo selectAll(int page,int limit) {
        //1.开启分页
        PageHelper.startPage(page, limit);
        //2.获取查询的结果
        PageInfo<UserDetailDto> pageInfo=new PageInfo<>(dao.selectAll());
        //3.返回结果
        return new PageBeanVo(pageInfo.getTotal(),pageInfo.getList());
    }
        @Override
    public R update(User user) {
            if (dao.update(user) > 0) {
                return R.ok();
            } else {
                return R.fail();
            }

        }
        @Override
    public R del(int id){
            if(dao.del(id)>0){
                return R.ok();
            }else {
                return R.fail();
            }
        }


    @Override
    public R login(UserLoginDto dto, HttpSession session) {
        //1.验证 验证码是否正确
        if(dto.getCode().equalsIgnoreCase(session.getAttribute("code").toString())){
            //2.查询数据库
            User user = dao.selectByName(dto.getName());
            //3.验证是否存在
            if (user != null) {
                //4.验证用户状态
                if (user.getFlag() == 1) {
                    //5.有效，再验证密码
                    if (user.getPassword().equals(dto.getPassword())) {
                        //6.登录成功
                        session.setAttribute("user", user);
                        user.setPassword("******");
                        return R.ok(user);
                    }
                }
            }
        }
        return R.fail();
    }

    @Override
    public R updatePass(String old, String newpassword, HttpSession session) {

            User user=(User) session.getAttribute("user");
            if(user!=null){
                if(dao.updatePass(user.getId(),newpassword,old)>0){
                    //修改成功
                    session.removeAttribute("user");
                    return R.ok();
                }
            }
            return R.fail();

    }


    @Override
    public R loginOut(HttpSession session) {
        session.removeAttribute("user");
        session.invalidate();
        return R.ok();
    }
}
